import axios from "axios";
import { ElMessage } from "element-plus";

const request = axios.create({
    baseURL: '/api',
    timeout: 5000
})

//请求拦截器
request.interceptors.request.use((config) => {
    return config;
})

//响应拦截器
request.interceptors.response.use((response) => {
    return response.data;
}, (error) => {
    //处理http网络错误
    if (error.response && error.response.status) {
    let status = error.response.status;
    switch (status) {
        case 404:
            ElMessage({
                type: 'error',
                message: error.message
            })
            break;
        case 500:
            ElMessage({
                type: 'error',
                message: '服务器炸了'
            })
            break;
        }
    }
    return Promise.reject(new Error(error.message))
})

export default request